<?php
class News extends AdminAction {

	function index() {
		template('news', $this->data);
	}

	function newslist() {
		$db = $this->getDB();
		$page = P('page');
		$rows = P('rows');
		$result = array ();
		$t = $db->get_assoc("select count(*) total from news where lang = '" . $this->lang . "'  ");
		$result['total'] = $t['total'];
		if (empty ($page) || empty ($rows)) {
			$r = $db->query_assoc("select id,title,date_format(createtime,'%Y-%c-%d') createtime from news where lang = '" . $this->lang . "' order by createtime desc limit 0,10");
			$result['rows'] = $r;
		} else {
			$start = ($page -1) * $rows;
			$r = $db->query_assoc("select id,title,date_format(createtime,'%Y-%c-%d') createtime from news where lang = '" . $this->lang . "' order by createtime desc limit $start,$rows ");
			$result['rows'] = $r;
		}
		echo json_encode($result);
	}

	function newsdelete() {
		$db = $this->getDB();
		$id = P('id');
		$db->query("delete from news where id = $id ");

		$message = array ();
		$message = setMessage(true, '删除成功！', $message);
		echo json_encode($message);
	}

	function newsadd() {
		$this->data['uuid'] = uuid2();
		template('newsadd', $this->data);
	}

	function newsadddo() {
		$db = $this->getDB();
		$imgids = P('imgid');
		$content = P('content');
		$content = addslashes($content);
		$title = P('title');
		$isdef = P('isdef');

		$db->query("insert into news (title,content,lang,createtime) values ('$title','$content','$this->lang',now())");
		$nid = $db->insert_id();

		if(is_array($imgids)){
			foreach ($imgids as $imgid){
				if($isdef == $imgid){
					$db->query("update news_images set newsid = '$nid',isdef=1 where id = $imgid");
				}else{
					$db->query("update news_images set newsid ='$nid',isdef=0 where id = $imgid");	
				}
			}
			
			if(empty($isdef)||$isdef==""){
				$fmid = $imgids[0];
				$db->query("update news_images set newsid = '$nid',isdef=1 where id = $fmid");	
			}
		}

		message('新闻添加成功！', '/admin.php/news/index');
	}

	
	
	/*
	function addnewsimg() {
		$db = $this->getDB();

		$file = $this->loadLib('file');
		$filename = $file->uploadfile('newsimg', 'attach/news');

		$db->query("insert into news_images (imgurl,createtime) values ('$filename',now())");
		$insertid = $db->insert_id();

		$message = array ();
		$message['insertid'] = $insertid;
		$message['location'] = $filename;
		$message = setMessage(true, '上传成功！', $message);
		echo json_encode($message);
	}*/

	function newsupdate() {
		$db = $this->getDB();
		$id = P('id');

		$news = $db->get_assoc("select * from news where id = '$id'");
		$this->data['news'] = $news;

		/*
		
		$newsimgs = $db->query_assoc("select * from news_images where newsid = '$id'");
		$this->data['newsimgs'] = $newsimgs;
		  
		$imgids = "";
		if (sizeof($newsimgs) >= 1) {
			$i = 1;
			foreach ($newsimgs as $img) {
				if ($i == sizeof($newsimgs)) {
					$imgids .= $img['id'];
				} else {
					$imgids .= $img['id'] . ',';
				}
			}
		}
		$this->data['imgids'] = $imgids;*/

		template('newsupdate', $this->data);
	}

	function newsupdatedo() {
		$db = $this->getDB();
		$sid = P('id');
		$attachids = P('attachids');
		$title = P('title');
		$content = P('content');
		$content = addslashes($content);

		$ids = array ();
		if ($attachids != "") {
			if (strpos($attachids, ',')) {
				$ids = explode(',', $attachids);
			} else {
				$ids = $attachids;
			}

			if (is_array($ids)) {
				if (sizeof($ids) >= 1) {
					foreach ($ids as $id) {
						$db->query("update news_images set newsid = $sid where id = $id");
					}
				}
			} else {
				$db->query("update news_images set newsid = $sid where id = $ids");
			}

		}
		if ($attachids != "") {
			$db->query("update news set content = '$content',title='$title',haveimg=1 where id  = $sid");
		} else {
			$db->query("update news set content = '$content',title='$title' where id  = $sid");
		}

		message('新闻编辑成功！', '/admin.php/news/index');
	}
	/**
	 * 添加图片
	 */
	function addnewsimg() {
		$db = $this->getDB();
		$newsid = P('newsid');

		$file = $this->loadLib('file');
		$filename = $file->uploadfile('newsimg', 'attach/news');

		$db->query("insert into news_images (imgurl,createtime,newsid) values ('$filename',now(),'$newsid')");

		$message = array ();
		$message = setMessage(true, '上传成功！', $message);
		echo json_encode($message);
	}
	/**
	 * delete图片
	 */
	function nimgdelete(){
		$db=$this->getDB();
		
		$imgid = P('imgid');
		$newsimg = $db->get_assoc("select * from news_images where id = $imgid");
		
		$file = $this->loadLib('file');
		$file ->unlink($newsimg['imgurl']);
		
		$db->query("delete from news_images where id = $imgid");
		
		$message = array();
		$message = setMessage(true,'文件删除成功！',$message);
		echo json_encode($message);
	}

	function nimglist(){
		$db=$this->getDB();
		$nid = P('nid');
		$page = P('page');
		$rows = P('rows');
		$result = array();
		$t = $db->get_assoc("select count(*) total from news_images where newsid = '$nid' ");
		$result['total'] = $t['total'];
		if(empty($page)||empty($rows)){
			$r = $db->query_assoc("select id,imgurl,date_format(createtime,'%Y-%c-%d') createtime,isdef from news_images  where newsid = '$nid'  order by createtime desc limit 0,10");
			$result['rows'] = $r;
		}else{
			$start = ($page-1)*$rows;
			$r = $db->query_assoc("select id,imgurl,date_format(createtime,'%Y-%c-%d') createtime,isdef from news_images  where newsid = '$nid'  order by createtime desc limit $start,$rows ");
			$result['rows'] = $r;
		}
		echo json_encode($result);
		
	}
	
	function getimglist(){
		$db=$this->getDB();
		$uuid = P('uuid');
		$page = P('page');
		$rows = P('rows');
		$result = array();
		$t = $db->get_assoc("select count(*) total from news_images where pid = '$uuid' ");
		$result['total'] = $t['total'];
		if(empty($page)||empty($rows)){
			$r = $db->query_assoc("select id,imgurl,date_format(createtime,'%Y-%c-%d') createtime,isdef from news_images  where newsid = '$uuid'  order by createtime desc limit 0,10");
			$result['rows'] = $r;
		}else{
			$start = ($page-1)*$rows;
			$r = $db->query_assoc("select id,imgurl,date_format(createtime,'%Y-%c-%d') createtime,isdef from news_images  where newsid = '$uuid'  order by createtime desc limit $start,$rows ");
			$result['rows'] = $r;
		}
		echo json_encode($result);
		
	}
}